﻿using System;

namespace InseSoft.Model.Entities
{
    public abstract class EntidadBase
    {
        #region Declarations

        protected string nombre;
        protected bool isChanged;
        private bool shouldCheckLength;

        #endregion

        #region Constructors

        protected EntidadBase()
        {
            shouldCheckLength = false;
        }

        #endregion

        #region Properties

        public virtual int Id { get; set; }

        public virtual bool ShouldCheckLength
        {
            get { return shouldCheckLength; }
            set { shouldCheckLength = value; }
        }

        public virtual bool IsChanged
        {
            get { return isChanged; }
        }

        #endregion

        #region Methods

        protected void CheckLength(string propertyName, string value, int maximum)
        {
            if (!ShouldCheckLength) return;

            if (!string.IsNullOrEmpty(value) && value.Length > maximum)
                throw new ArgumentOutOfRangeException(value, value, string.Format("Valor inválido para {0}", propertyName));
        }

        #region Equals And HashCode Overrides

        public override bool Equals(object obj)
        {
            if (this == obj) return true;
            if ((obj == null) || (obj.GetType() != GetType())) return false;
            var castObj = (EntidadBase)obj;
            return Id == castObj.Id;
        }

        public override int GetHashCode()
        {
            var hash = 57;
            hash = 27 * hash * Id.GetHashCode();
            return hash;
        }

        #endregion

        #endregion
    }
}
